<template>
  <div class="app">
    <div v-if="hasToken" class="avart">
      <!-- 已经登录的内容 -->
      <div class="backgr">
        <img src="../../assets/user/userBeiJingTu.png" alt="">
      </div>
      <div class="overlay" @click="onuserDetails">
        <img :src="user.avatar" alt="User Avatar">
        <p>{{user.username}}</p>
      </div>
    </div>
    <div v-else class="avart">
      <!-- 没有登录的提示 -->
      <div class="backgr blur">
        <img src="../../assets/user/userBeiJingTu.png" alt="">
      </div>
      <div class="overlay">
      <p style="">您还没有登录,请先登录哦</p>
      <router-link to="/login" style="">去登录</router-link></div>
    </div>
  </div>
</template>

<script>
export default {
  name: "myUser",
  components: {},
  data() {
    return {
      myname1: "罗德宝",
      myname2: "王源生",
      user:[],
      hasToken: false, // 添加一个状态记录是否存在 token
    };
  },
  mounted() {
    // 判断是否存在 token
    if (window.sessionStorage.getItem("token")) {
      this.hasToken = true;
    }
     // 获取数据
     const userStr = window.sessionStorage.getItem("user");
        // console.log(userStr); // 检查返回的字符串是否是有效的 JSON 字符串
        this.user = JSON.parse(userStr);

  },
  methods: {
    onuserDetails() {
      this.$router.push("/userDetails");
    },
  },
};
</script>

<style scoped>
.app {
  display: flex;
  justify-content: center;
}


.avart {
  position: relative;
}

.backgr img {
  width: 100%;
  height: auto;
  display: block;
}

.overlay {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -10%);
  text-align: center;
}

.overlay img {
  width: 60px;
  height: 60px;
  border-radius: 50%;
}

.overlay p {
  margin: 5px;
  font-weight: bold;
}
.blur {
  filter: blur(5px);
}
</style>